﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using HeadLinx.AD.Explorer.Properties;

namespace HeadLinx.AD.Explorer
{
    /// <summary>
    /// Interaction logic for PhotoCacheSettingsDialog.xaml
    /// </summary>
    public partial class PhotoCacheSettingsDialog : Window
    {
        public PhotoCacheSettingsDialog()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            int expireDays = Settings.Default.CachedPhotoExpireDays;

            if (expireDays == 14)
            {
                cbxExpireDuration.SelectedIndex = 0;
            }
            else if (expireDays == 30)
            {
                cbxExpireDuration.SelectedIndex = 1;
            }
            else if (expireDays == 0)
            {
                // "Never"
                cbxExpireDuration.SelectedIndex = 2;
            }
            else
            {
                const int defaultExpireDays = 30;
                Settings.Default.CachedPhotoExpireDays = defaultExpireDays;
                Settings.Default.Save();
                cbxExpireDuration.SelectedIndex = 1;
            }
        }

        private void btnBrowseCache_Click(object sender, RoutedEventArgs e)
        {
            if (App.PhotoCache != null)
            {
                Process.Start(App.PhotoCache.CacheFolder);
            }
        }

        private void btnClearCache_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(
                "To clear the photo cache, click the Browse Cache button then use Windows Explorer to remove cached photos.",
                "Clear Photo Cache");
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (cbxExpireDuration.SelectedIndex == 0)
            {
                Settings.Default.CachedPhotoExpireDays = 14;
            }
            else if (cbxExpireDuration.SelectedIndex == 1)
            {
                Settings.Default.CachedPhotoExpireDays = 30;
            }
            else if (cbxExpireDuration.SelectedIndex == 2)
            {
                Settings.Default.CachedPhotoExpireDays = 0;
            }
            else
            {
                Settings.Default.CachedPhotoExpireDays = 30;
            }

            Settings.Default.Save();
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
